/*
 * net_broadcast.h
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 */

#ifndef NET_BRORDCAST_H_
#define NET_BROADCAST_H_

#include "RBFCommon.h"

#define xCONFIG_LOG_RBFBROADCAST // 模块日志宏定义,使能,模块输出日志

#define __packed __attribute__((__packed__))

#define GET_BRETRY(bretryAckCtrl) (bretryAckCtrl & 0x07)
#define GET_ACKCTRL(bretryAckCtrl) ((bretryAckCtrl & 0xF8) >> 3)
#define GET_BRETRYACKCTRL(bretry, AckCtrl) ((bretry & 0x07) | ((AckCtrl & 0xF8) << 3))

/*
 * cmd = 0x08  0x09 广播
 * payload: SystemID(3B) + BSeq(1B) + Bretry/AckCtrl(1B) + BTypeDate(nB)
 *
 */
typedef struct
{
  uint32_t systemId : 24; //[16]
  uint8_t bretryAckCtrl;  //[20]
  uint8_t bTypeDataSize;
  uint8_t *bTypeData;
} __packed RBFMacMessageGWBoardcast_t;

typedef enum
{
  RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_SYSTEMID = RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_FIRST,
  RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_BRETRYACKCTRL = RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_BTYPEDATA = RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_LAST = RBF_MAC_PAYLOAD_GW_BROADCAST_INDEX_BTYPEDATA,
} RBFMAC_PAYLOAD_GW_BROADCAST_INDEX_E;

#define RBF_MAC_PAYLOAD_GW_BROADCAST_LEN (sizeof(RBFMacMessageGWBoardcast_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GW_BROADCAST_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_BROADCAST_LEN)
#else
#define RBF_MAC_PACKET_GW_BROADCAST_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_BROADCAST_LEN)
#endif

/*
 * 网关广播payload
 */
// 解包
void RBF_gw_broadcast_payload_packetToMsg(uint8_t *packet, RBFMacMessageGWBoardcast_t *msg);
// 组包
void RBF_gw_broadcast_payload_msgToPacket(RBFMacMessageGWBoardcast_t msg, uint8_t *packet);

/***********************广播类型/物模型******************************/

#define BTYPE_IO_EN_DATA_LEN 16
#define BTYPE_SOUNDER_DATA_LEN 15
#define BTYPE_KEYPAD_DATA_LEN 10
#define BTYPE_SWITCH_DATA_LEN NULL
#define BTYPE_SOUNDER_CTRL_DATA_LEN NULL
#define BTYPE_KEYPAD_CTRL_DATA_LEN NULL
#define BTYPE_SWITCH_CTRL_DATA_LEN NULL
#define BTYPE_GROUP_DATA_LEN NULL
#define BTYPE_UPDATEME_DATA_LEN NULL
#define BTYPE_GROUP_SET_DATA_LEN NULL

typedef enum
{
  BTYPE_NONE = 0,
  BTYPE_IO_EN = 0x01,
  BTYPE_SOUNDER,
  BTYPE_KEYPAD,
  BTYPE_SWITCH,
  // BTYPE_SOUNDER_CTRL,
  BTYPE_KEYPAD_CTRL,
  BTYPE_SWITCH_CTRL,
  BTYPE_GROUP,
  BTYPE_UPDATEME = 0x08,
  BTYPE_GROUP_SET = 0x41,
} BTYPE_E;

#endif /* NET_BOARDCAST_H_ */
